package UPPM;

import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import UPPM.Utils;

public class TestAccountDetails {

	WebDriver driver = new FirefoxDriver();

	@Before
	public void setUp() throws Exception {
		driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		driver.get("http://test.unippmtest.co.uk");
//		Thread.sleep(3000);
//		Utils.acceptCookies(driver);
				
	}

@Test
	public void testAccountDetailsInitialState() throws Exception {
	
		Utils.Login(driver);
		System.out.println("testAccountDetailsInitialState");
		System.getProperty("line.separator");
		
		Utils.waitForElement(driver, By.className("dropdown"));
		WebElement acc_details = driver.findElement(By.xpath(".//*[@id='login-view']/ul/li[4]/a"));
		acc_details.click(); 
		
		WebElement click_value = driver.findElement(By.linkText("My Account Details"));
		click_value.click();
		Thread.sleep(1000);
		
		//check account details fields
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("title"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("firstname"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("lastname"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("emailField"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("password"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("confirmPassword"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("companyname"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("jobtitle"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("address1"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("address2"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("city"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("postcode"))); 
		
		
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("countrydropdown"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("state"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("phonenumber"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("faxnumber"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("mobilenumber"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("website"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("businesstypedropdown"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("newsletter"))); 
    	Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("defaultlanguagedropdown"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("defaultlabeldropdown"))); 

		WebElement update_btn = driver.findElement(By.id("updatedetails"));
		Assert.assertEquals(update_btn.isEnabled(), true);
		
		//check left menu options
		WebElement my_acc_details = driver.findElement(By.xpath(".//*[@id='edit-details']")); 
		Assert.assertEquals("My Account Details", my_acc_details.getText());
		WebElement my_dld_pref = driver.findElement(By.xpath(".//*[@id='download-preferences']")); 
		Assert.assertEquals("My Download Preferences", my_dld_pref.getText());
		WebElement search_hist = driver.findElement(By.xpath(".//*[@id='search-history']"));
		Assert.assertEquals("My Search History", search_hist.getText());
		WebElement audition_hist = driver.findElement(By.xpath(".//*[@id='audition-history']"));
		Assert.assertEquals("My Audition History", audition_hist.getText());
		WebElement dld_hist = driver.findElement(By.xpath(".//*[@id='User Download History']"));
		Assert.assertEquals("My Download History", dld_hist.getText());
		WebElement buddies = driver.findElement(By.xpath(".//*[@id='manage-buddies']"));
		Assert.assertEquals("Manage Buddies", buddies.getText());
		WebElement fdb_form = driver.findElement(By.xpath(".//*[@id='Feedback Form']"));
		Assert.assertEquals("Feedback Form", fdb_form.getText());

	}

	@Test
	public void testUpdateAccountDetailsFunctionality() throws Exception {
		
		Utils.Login(driver);
		System.out.println("testUpdateAccountDetailsFunctionality");
		System.getProperty("line.separator");
			
		Utils.waitForElement(driver, By.className("dropdown"));
		WebElement acc_details = driver.findElement(By.xpath(".//*[@id='login-view']/ul/li[4]/a"));
		acc_details.click(); 
		
		WebElement click_value = driver.findElement(By.linkText("My Account Details"));
		click_value.click();
		Thread.sleep(1000);
		
		//update a couple of fields
		WebElement title = driver.findElement(By.id("title"));
		title.clear();
		title.sendKeys("test");Thread.sleep(1000);
		
		WebElement companyname = driver.findElement(By.id("companyname"));
		companyname.clear();
		companyname.sendKeys("test");Thread.sleep(1000);
		
		WebElement jobtitle = driver.findElement(By.id("jobtitle"));
		jobtitle.clear();
		jobtitle.sendKeys("test");Thread.sleep(1000);

		WebElement update_btn = driver.findElement(By.id("updatedetails"));
		update_btn.click();
		Thread.sleep(2000);
		
		Utils.waitForElement(driver, By.xpath("//*[@id='phcentralrow_1_phcentraltwocolumnright_1_fields']/p"));
		WebElement msg = driver.findElement(By.xpath("//*[@id='phcentralrow_1_phcentraltwocolumnright_1_fields']/p"));
		Assert.assertEquals("You have successfully edited your details.", msg.getText());
	}
	
	@Test
	public void testMyDownloadSettings() throws Exception {
		
		Utils.Login(driver);
		System.out.println("TestAccountDetails>testMyDownloadSettings");
		System.getProperty("line.separator");
		
		Utils.waitForElement(driver, By.className("dropdown"));
		WebElement acc_details = driver.findElement(By.xpath(".//*[@id='login-view']/ul/li[4]/a"));
		acc_details.click(); 
		
		Utils.waitForElement(driver, By.linkText("My Download Preferences"));
		WebElement download_preferences = driver.findElement(By.linkText("My Download Preferences"));
		download_preferences.click();
				
		Select droplist = new Select (driver.findElement(By.id("defaultDownloadFormat")));
		droplist.selectByVisibleText("mp3");
		
		//check update btn
		WebElement update_btn = driver.findElement(By.id("updatebutton"));
		Assert.assertEquals(true, update_btn.isEnabled());
		
	}
	
	@Test
	public void testManageBuddies() throws Exception {
		
		Utils.Login(driver);
		System.out.println("TestAccountDetails>testManageBuddies");
		System.getProperty("line.separator");
		
		Utils.waitForElement(driver, By.className("dropdown"));
		WebElement acc_details = driver.findElement(By.xpath(".//*[@id='login-view']/ul/li[4]/a"));
		acc_details.click(); 
		
		
		Utils.waitForElement(driver, By.linkText("Manage Buddies"));
		WebElement buddies = driver.findElement(By.linkText("Manage Buddies"));
		buddies.click();
		
		//check fields 
		//add a buddy that is not registered
		//and check error message
		WebElement buddy_name = driver.findElement(By.id("name"));
		Assert.assertEquals(true, buddy_name.isEnabled());
		buddy_name.sendKeys("test");
		WebElement buddy_email = driver.findElement(By.id("email"));
		Assert.assertEquals(true, buddy_email.isDisplayed());
		buddy_email.sendKeys("test//@Test.ro");
		WebElement add_btn = driver.findElement(By.id("addbuddy"));
		add_btn.click();
		Utils.waitForElement(driver, By.id("errorMsg"));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("sortbuddies"))); 

	}
	
	@Test
	public void testMyAuditionHistory() throws Exception {
		
		Utils.Login(driver);
		System.out.println("TestAccountDetails>testMyAuditionHistory");
		System.getProperty("line.separator");
		
		Utils.waitForElement(driver, By.className("dropdown"));
		WebElement acc_details = driver.findElement(By.xpath(".//*[@id='login-view']/ul/li[4]/a"));
		acc_details.click(); 
		
		
		Utils.waitForElement(driver, By.linkText("My Audition History"));
		WebElement audition_history = driver.findElement(By.linkText("My Audition History"));
		audition_history.click();
		
				
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("sort"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("results_num"))); 
		
	}
	
	
	@Test
	public void testMySearchHistory() throws Exception {
		
		Utils.Login(driver);
		System.out.println("TestAccountDetails>testMySearchHistory");
		System.getProperty("line.separator");
		
		Utils.waitForElement(driver, By.className("dropdown"));
		WebElement acc_details = driver.findElement(By.xpath(".//*[@id='login-view']/ul/li[4]/a"));
		acc_details.click(); 
		
		
		
		Utils.waitForElement(driver, By.linkText("My Search History"));
		WebElement search_history = driver.findElement(By.linkText("My Search History"));
		search_history.click();
		
		Thread.sleep(3000);
		WebElement search_hist = driver.findElement(By.id("search-history"));
		search_hist.click();
		
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("search_hist_num"))); 
	
	}
	
	@Test
	public void testMyDownloadHistory() throws Exception {
		
		Utils.Login(driver);
		System.out.println("TestAccountDetails>testMyDownloadHistory");
		System.getProperty("line.separator");
		
		Utils.waitForElement(driver, By.className("dropdown"));
		WebElement acc_details = driver.findElement(By.xpath(".//*[@id='login-view']/ul/li[4]/a"));
		acc_details.click(); 
				
		
		Utils.waitForElement(driver, By.linkText("My Download History"));
		WebElement download_history = driver.findElement(By.linkText("My Download History"));
		download_history.click();
		
		
		WebElement dld_hist = driver.findElement(By.id("User Download History"));
		dld_hist.click();
		
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("sort")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("results_num")));
		
	}
	
	@Test
	public void testFeedbackForm() throws Exception {
		
		Utils.Login(driver);
		System.out.println("TestAccountDetails>testFeedbackForm");
		System.getProperty("line.separator");
		Utils.waitForElement(driver, By.className("dropdown"));
		WebElement acc_details = driver.findElement(By.xpath(".//*[@id='login-view']/ul/li[4]/a"));
		acc_details.click(); 
		
		WebElement click_value = driver.findElement(By.linkText("My Account Details"));
		click_value.click();
		Thread.sleep(1000);
		
		WebElement fdb_form = driver.findElement(By.id("Feedback Form"));
		fdb_form.click();
		
		WebElement email = driver.findElement(By.id("sendtodorpdown"));
		Assert.assertEquals(email.isEnabled(), true);
		WebElement name = driver.findElement(By.id("subject"));
		Assert.assertEquals(name.isEnabled(), true);
		WebElement msg = driver.findElement(By.id("body")); 
		Assert.assertEquals(msg.isEnabled(), true);
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("submitfeedback")));
				
	}
	
	@After
	public void tearDown() throws Exception {
		driver.quit();
	}
}


